package com.pybox.spec;

import com.pybox.entity.TWeekTime;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import java.util.*;


/**
* 描述：TWeekTime Specification
* @author wuyingkai
* @date 2018-09-15
*/


public class TWeekTimeSpec implements Specification<TWeekTime> {

    private Map<String, String> params;

    public TWeekTimeSpec(Map<String, String> params){
        this.params = params;
    }

    @Override
    public Predicate toPredicate(Root<TWeekTime> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {


        String groupId = this.params.get("groupId");
        String dayofweek = this.params.get("dayofweek");
        String hour0 = this.params.get("hour0");
        String hour1 = this.params.get("hour1");
        String hour2 = this.params.get("hour2");
        String hour3 = this.params.get("hour3");
        String hour4 = this.params.get("hour4");
        String hour5 = this.params.get("hour5");
        String hour6 = this.params.get("hour6");
        String hour7 = this.params.get("hour7");
        String hour8 = this.params.get("hour8");
        String hour9 = this.params.get("hour9");
        String hour10 = this.params.get("hour10");
        String hour11 = this.params.get("hour11");
        String hour12 = this.params.get("hour12");
        String hour13 = this.params.get("hour13");
        String hour14 = this.params.get("hour14");
        String hour15 = this.params.get("hour15");
        String hour16 = this.params.get("hour16");
        String hour17 = this.params.get("hour17");
        String hour18 = this.params.get("hour18");
        String hour19 = this.params.get("hour19");
        String hour20 = this.params.get("hour20");
        String hour21 = this.params.get("hour21");
        String hour22 = this.params.get("hour22");
        String hour23 = this.params.get("hour23");
        String ctime = this.params.get("ctime");
        String utime = this.params.get("utime");
        List<Predicate> ps = new ArrayList<>();


        if (!StringUtils.isEmpty(groupId)) {
            ps.add(criteriaBuilder.like(root.get("groupId"), "%" + groupId + "%"));
        }
        if (!StringUtils.isEmpty(dayofweek)) {
            ps.add(criteriaBuilder.like(root.get("dayofweek"), "%" + dayofweek + "%"));
        }
        if (!StringUtils.isEmpty(hour0)) {
            ps.add(criteriaBuilder.like(root.get("hour0"), "%" + hour0 + "%"));
        }
        if (!StringUtils.isEmpty(hour1)) {
            ps.add(criteriaBuilder.like(root.get("hour1"), "%" + hour1 + "%"));
        }
        if (!StringUtils.isEmpty(hour2)) {
            ps.add(criteriaBuilder.like(root.get("hour2"), "%" + hour2 + "%"));
        }
        if (!StringUtils.isEmpty(hour3)) {
            ps.add(criteriaBuilder.like(root.get("hour3"), "%" + hour3 + "%"));
        }
        if (!StringUtils.isEmpty(hour4)) {
            ps.add(criteriaBuilder.like(root.get("hour4"), "%" + hour4 + "%"));
        }
        if (!StringUtils.isEmpty(hour5)) {
            ps.add(criteriaBuilder.like(root.get("hour5"), "%" + hour5 + "%"));
        }
        if (!StringUtils.isEmpty(hour6)) {
            ps.add(criteriaBuilder.like(root.get("hour6"), "%" + hour6 + "%"));
        }
        if (!StringUtils.isEmpty(hour7)) {
            ps.add(criteriaBuilder.like(root.get("hour7"), "%" + hour7 + "%"));
        }
        if (!StringUtils.isEmpty(hour8)) {
            ps.add(criteriaBuilder.like(root.get("hour8"), "%" + hour8 + "%"));
        }
        if (!StringUtils.isEmpty(hour9)) {
            ps.add(criteriaBuilder.like(root.get("hour9"), "%" + hour9 + "%"));
        }
        if (!StringUtils.isEmpty(hour10)) {
            ps.add(criteriaBuilder.like(root.get("hour10"), "%" + hour10 + "%"));
        }
        if (!StringUtils.isEmpty(hour11)) {
            ps.add(criteriaBuilder.like(root.get("hour11"), "%" + hour11 + "%"));
        }
        if (!StringUtils.isEmpty(hour12)) {
            ps.add(criteriaBuilder.like(root.get("hour12"), "%" + hour12 + "%"));
        }
        if (!StringUtils.isEmpty(hour13)) {
            ps.add(criteriaBuilder.like(root.get("hour13"), "%" + hour13 + "%"));
        }
        if (!StringUtils.isEmpty(hour14)) {
            ps.add(criteriaBuilder.like(root.get("hour14"), "%" + hour14 + "%"));
        }
        if (!StringUtils.isEmpty(hour15)) {
            ps.add(criteriaBuilder.like(root.get("hour15"), "%" + hour15 + "%"));
        }
        if (!StringUtils.isEmpty(hour16)) {
            ps.add(criteriaBuilder.like(root.get("hour16"), "%" + hour16 + "%"));
        }
        if (!StringUtils.isEmpty(hour17)) {
            ps.add(criteriaBuilder.like(root.get("hour17"), "%" + hour17 + "%"));
        }
        if (!StringUtils.isEmpty(hour18)) {
            ps.add(criteriaBuilder.like(root.get("hour18"), "%" + hour18 + "%"));
        }
        if (!StringUtils.isEmpty(hour19)) {
            ps.add(criteriaBuilder.like(root.get("hour19"), "%" + hour19 + "%"));
        }
        if (!StringUtils.isEmpty(hour20)) {
            ps.add(criteriaBuilder.like(root.get("hour20"), "%" + hour20 + "%"));
        }
        if (!StringUtils.isEmpty(hour21)) {
            ps.add(criteriaBuilder.like(root.get("hour21"), "%" + hour21 + "%"));
        }
        if (!StringUtils.isEmpty(hour22)) {
            ps.add(criteriaBuilder.like(root.get("hour22"), "%" + hour22 + "%"));
        }
        if (!StringUtils.isEmpty(hour23)) {
            ps.add(criteriaBuilder.like(root.get("hour23"), "%" + hour23 + "%"));
        }
        if (!StringUtils.isEmpty(ctime)) {
            ps.add(criteriaBuilder.like(root.get("ctime"), "%" + ctime + "%"));
        }
        if (!StringUtils.isEmpty(utime)) {
            ps.add(criteriaBuilder.like(root.get("utime"), "%" + utime + "%"));
        }

        criteriaQuery = criteriaQuery.where(ps.toArray(new Predicate[ps.size()]));

        return criteriaQuery.getRestriction();
    }

}